package fr.fermeManager.application.views.components;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;

import javax.swing.JCheckBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import org.jdesktop.beansbinding.AutoBinding;
import org.jdesktop.beansbinding.AutoBinding.UpdateStrategy;
import org.jdesktop.beansbinding.BeanProperty;
import org.jdesktop.beansbinding.Binding;
import org.jdesktop.beansbinding.BindingGroup;
import org.jdesktop.beansbinding.Bindings;

import fr.fermeManager.application.utils.AppContext;
import fr.fermeManager.application.utils.Constantes;
import fr.fermeManager.library.model.beans.TypeTransaction;

public class ComponentTypeTransaction extends JPanel {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private BindingGroup m_bindingGroup;
	private TypeTransaction typeTransaction = new TypeTransaction(true);
	private JCheckBox actifJCheckBox;
	private JTextField descritptionJTextField;
	private JTextField idJTextField;
	private JTextField idParentJTextField;
	private JTextField libelleJTextField;

	public ComponentTypeTransaction(TypeTransaction newTypeTransaction) {
		this();
		setTypeTransaction(newTypeTransaction,false);
	}

	public ComponentTypeTransaction() {
		GridBagLayout gridBagLayout = new GridBagLayout();
		gridBagLayout.columnWidths = new int[] { 105, 0, 0 };
		gridBagLayout.rowHeights = new int[] { 0, 0, 0, 0, 0, 0 };
		gridBagLayout.columnWeights = new double[] { 0.0, 1.0, 1.0E-4 };
		gridBagLayout.rowWeights = new double[] { 1.0, 1.0, 1.0, 1.0, 1.0,
				1.0E-4 };
		setLayout(gridBagLayout);

		JLabel actifLabel = new JLabel("Actif:");
		GridBagConstraints labelGbc_0 = new GridBagConstraints();
		labelGbc_0.insets = new Insets(5, 5, 5, 5);
		labelGbc_0.gridx = 0;
		labelGbc_0.gridy = 0;
		add(actifLabel, labelGbc_0);

		actifJCheckBox = new JCheckBox();
		GridBagConstraints componentGbc_0 = new GridBagConstraints();
		componentGbc_0.insets = new Insets(5, 0, 5, 0);
		componentGbc_0.fill = GridBagConstraints.HORIZONTAL;
		componentGbc_0.gridx = 1;
		componentGbc_0.gridy = 0;
		add(actifJCheckBox, componentGbc_0);
				
						JLabel idLabel = new JLabel("Id:");
						GridBagConstraints labelGbc_2 = new GridBagConstraints();
						labelGbc_2.insets = new Insets(5, 5, 5, 5);
						labelGbc_2.gridx = 0;
						labelGbc_2.gridy = 1;
						add(idLabel, labelGbc_2);
				
						idJTextField = new JTextField();
						idJTextField.setEditable(false);
						GridBagConstraints componentGbc_2 = new GridBagConstraints();
						componentGbc_2.insets = new Insets(5, 0, 5, 0);
						componentGbc_2.fill = GridBagConstraints.HORIZONTAL;
						componentGbc_2.gridx = 1;
						componentGbc_2.gridy = 1;
						add(idJTextField, componentGbc_2);
				
						JLabel idParentLabel = new JLabel("IdParent:");
						GridBagConstraints labelGbc_3 = new GridBagConstraints();
						labelGbc_3.insets = new Insets(5, 5, 5, 5);
						labelGbc_3.gridx = 0;
						labelGbc_3.gridy = 2;
						add(idParentLabel, labelGbc_3);
				
						idParentJTextField = new JTextField();
						idParentJTextField.setEditable(false);
						GridBagConstraints componentGbc_3 = new GridBagConstraints();
						componentGbc_3.insets = new Insets(5, 0, 5, 0);
						componentGbc_3.fill = GridBagConstraints.HORIZONTAL;
						componentGbc_3.gridx = 1;
						componentGbc_3.gridy = 2;
						add(idParentJTextField, componentGbc_3);
		
				JLabel libelleLabel = new JLabel("Libelle:");
				GridBagConstraints labelGbc_4 = new GridBagConstraints();
				labelGbc_4.insets = new Insets(5, 5, 5, 5);
				labelGbc_4.gridx = 0;
				labelGbc_4.gridy = 3;
				add(libelleLabel, labelGbc_4);
		
				libelleJTextField = new JTextField();
				GridBagConstraints componentGbc_4 = new GridBagConstraints();
				componentGbc_4.insets = new Insets(5, 0, 5, 0);
				componentGbc_4.fill = GridBagConstraints.HORIZONTAL;
				componentGbc_4.gridx = 1;
				componentGbc_4.gridy = 3;
				add(libelleJTextField, componentGbc_4);

		JLabel descritptionLabel = new JLabel("Descritption:");
		GridBagConstraints labelGbc_1 = new GridBagConstraints();
		labelGbc_1.insets = new Insets(5, 5, 0, 5);
		labelGbc_1.gridx = 0;
		labelGbc_1.gridy = 4;
		add(descritptionLabel, labelGbc_1);

		descritptionJTextField = new JTextField();
		GridBagConstraints componentGbc_1 = new GridBagConstraints();
		componentGbc_1.insets = new Insets(5, 0, 0, 0);
		componentGbc_1.fill = GridBagConstraints.HORIZONTAL;
		componentGbc_1.gridx = 1;
		componentGbc_1.gridy = 4;
		add(descritptionJTextField, componentGbc_1);

		if (typeTransaction != null) {
			m_bindingGroup = initDataBindings();
		}
	}

	public TypeTransaction getTypeTransaction() {
		return typeTransaction;
	}

	public void setTypeTransaction(TypeTransaction newTypeTransaction) {
		String currentUser =  (String) AppContext.getSession().get(Constantes.USER);
		actifJCheckBox.setEnabled(false);
		if(Constantes.ADMIN.equalsIgnoreCase(currentUser)){
			actifJCheckBox.setEnabled(true);
		}
		setTypeTransaction(newTypeTransaction, true);
	}

	public void setTypeTransaction(TypeTransaction newTypeTransaction,boolean update) {
		typeTransaction = newTypeTransaction;
		if (update) {
			if (m_bindingGroup != null) {
				m_bindingGroup.unbind();
				m_bindingGroup = null;
			}
			if (typeTransaction != null) {
				m_bindingGroup = initDataBindings();
			}
		}
	}
	
	public void sauvegarder(){
		for (@SuppressWarnings("rawtypes") Binding bindingTmp : m_bindingGroup.getBindings()) {
			bindingTmp.unbind();
			bindingTmp.save();
			bindingTmp.bind();
		}
	}
	
	public void recharger(){
		for (@SuppressWarnings("rawtypes") Binding bindingTmp : m_bindingGroup.getBindings()) {
			bindingTmp.unbind();
			bindingTmp.refresh();
			bindingTmp.bind();
		}
	}
	protected BindingGroup initDataBindings() {
		BeanProperty<TypeTransaction, Boolean> actifProperty = BeanProperty.create("actif");
		BeanProperty<JCheckBox, Boolean> selectedProperty = BeanProperty.create("selected");
		AutoBinding<TypeTransaction, Boolean, JCheckBox, Boolean> autoBinding = Bindings.createAutoBinding(UpdateStrategy.READ_ONCE, typeTransaction, actifProperty, actifJCheckBox, selectedProperty);
		autoBinding.bind();
		//
		BeanProperty<TypeTransaction, String> descritptionProperty = BeanProperty.create("descritption");
		BeanProperty<JTextField, String> textProperty = BeanProperty.create("text");
		AutoBinding<TypeTransaction, String, JTextField, String> autoBinding_1 = Bindings.createAutoBinding(UpdateStrategy.READ_ONCE, typeTransaction, descritptionProperty, descritptionJTextField, textProperty);
		autoBinding_1.bind();
		//
		BeanProperty<TypeTransaction, Integer> idProperty = BeanProperty.create("id");
		BeanProperty<JTextField, String> textProperty_1 = BeanProperty.create("text");
		AutoBinding<TypeTransaction, Integer, JTextField, String> autoBinding_2 = Bindings.createAutoBinding(UpdateStrategy.READ_ONCE, typeTransaction, idProperty, idJTextField, textProperty_1);
		autoBinding_2.bind();
		//
		BeanProperty<TypeTransaction, Integer> idParentProperty = BeanProperty.create("idParent");
		BeanProperty<JTextField, String> textProperty_2 = BeanProperty.create("text");
		AutoBinding<TypeTransaction, Integer, JTextField, String> autoBinding_3 = Bindings.createAutoBinding(UpdateStrategy.READ_ONCE, typeTransaction, idParentProperty, idParentJTextField, textProperty_2);
		autoBinding_3.bind();
		//
		BeanProperty<TypeTransaction, String> libelleProperty = BeanProperty.create("libelle");
		BeanProperty<JTextField, String> textProperty_3 = BeanProperty.create("text");
		AutoBinding<TypeTransaction, String, JTextField, String> autoBinding_4 = Bindings.createAutoBinding(UpdateStrategy.READ_ONCE, typeTransaction, libelleProperty, libelleJTextField, textProperty_3);
		autoBinding_4.bind();
		//
		BindingGroup bindingGroup = new BindingGroup();
		//
		bindingGroup.addBinding(autoBinding);
		bindingGroup.addBinding(autoBinding_1);
		bindingGroup.addBinding(autoBinding_2);
		bindingGroup.addBinding(autoBinding_3);
		bindingGroup.addBinding(autoBinding_4);
		return bindingGroup;
	}
	
	public String getDescritption(){
		return this.descritptionJTextField.getText();
	}
	
	public String getLibelle(){
		return this.libelleJTextField.getText();
	}
}
